﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Components;
using Base;
using System.ComponentModel;
using Core.Base;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Data;
using AjaxControlToolkit;
using ERP.BusinessEntities.Base;
using ERP.DAL;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace Core.Component.WebForm
{
    public class SGVComboBox:ComboBox, ISGVControl, IBindingControl
    {
        public SGVComboBox()
            : base()
        {
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this.CausesValidation = false;
        }

        public Core.Component.UIComponent component
        {
            set;
            get;
        }

        #region IBindingControl
        BaseModule module;

        String _bindingPropertyName = "SelectedValue";

        String _bindingTable;
        String _bindingColumn;
        String _dataSourceTable;
        String _dataSourceDisplayColumn;
        String _dataSourceKeyColumn;
        String _dataSourceColumns;
        String _sGVShortcutString = "F2: DUYỆT DANH SÁCH";
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String SGVShortcutString
        {
            get { return _sGVShortcutString; }
            set { _sGVShortcutString = value; }
        }
        String _sGVTag;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVTag
        {
            get
            {
                return _sGVTag;
            }
            set
            {
                _sGVTag = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        string IBindingControl.BindingPropertyName
        {
            get
            {
                return _bindingPropertyName;
            }
            set
            {
                _bindingPropertyName = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingTable
        {
            get
            {
                return _bindingTable;
            }
            set
            {
                _bindingTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingColumn
        {
            get
            {
                return _bindingColumn;
            }
            set
            {
                _bindingColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceTable
        {
            get
            {
                return _dataSourceTable;
            }
            set
            {
                _dataSourceTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceDisplayColumn
        {
            get
            {
                return _dataSourceDisplayColumn;
            }
            set
            {
                _dataSourceDisplayColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceKeyColumn
        {
            get
            {
                return _dataSourceKeyColumn;
            }
            set
            {
                _dataSourceKeyColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String DataSourceColumns
        {
            get
            {
                return _dataSourceColumns;
            }
            set
            {
                _dataSourceColumns = value;
            }
        }

        

        #endregion

        public void RegisterModule(Core.Base.BaseModule module)
        {
            this.module = module;
        }

        public void Init()
        {
            //this.DropDownStyle = ComboBoxStyle.DropDown;
            InitDataSource();
        }

        public virtual void InitDataSource()
        {
        }


        public void UpdateFormat()
        {
        }

        public UIComponent Serialize()
        {
            return null;
        }

        public void Deserialize(UIComponent component)
        {
            SGVControlHelper.PopulateProperties(this, component);

            String connecString = String.Format(@"Server={0};Database={1};User Id={2};Password={3};", "192.168.4.72", "SGERP", "sa", "110");
            //Microsoft.Practices.EnterpriseLibrary.Data.Database db = EnterpriseLibraryContainer.Current.GetInstance<Microsoft.Practices.EnterpriseLibrary.Data.Database>("Test");
            Microsoft.Practices.EnterpriseLibrary.Data.Database db = new SqlDatabase(connecString);
            Host Prov = new Host(db);

            BaseBusinessController Ctrl = Host.GetCtrl("ICStocks");

            DataSet ds = Ctrl.GetAllObjects();

            this.DataSource = ds.Tables[0];
            this.DataValueField = "ID";
            this.DataTextField = "No";
            this.DataBind();
        }
    }
}
